//
//  ContentViewController.m
//  UI18_Work 数据库
//
//  Created by Apple on 16/8/8.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ContentViewController.h"

@interface ContentViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>
@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *phoneNumTextField;
@property (nonatomic, strong) UITextField *addressTextField;
@property (nonatomic, strong) UIButton *changeButton;
@property (nonatomic, strong) NSMutableDictionary *textFieldDic;
@property (nonatomic, strong) NSString *picImageStr;
@end

@implementation ContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createContentView];

}
- (void)createContentView {
    // 创建label
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 240, 60, 30)];
    nameLabel.text = @"姓名";
    [self.view addSubview:nameLabel];
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 340, 60, 30)];
    phoneLabel.text = @"电话";
    [self.view addSubview:phoneLabel];
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 290, 60, 30)];
    addressLabel.text = @"地址";
    [self.view addSubview:addressLabel];
    
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 10, 150, 200)];
    self.picImageView.backgroundColor = [UIColor redColor];
    self.picImageView.image = [UIImage imageWithContentsOfFile:self.person.picImage];
    self.picImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.picImageView addGestureRecognizer:tap];
    [self.view addSubview:self.picImageView];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 240, 200, 30)];
    self.nameTextField.text = self.person.name;
    [self.view addSubview:self.nameTextField];
    
    self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 290, 200, 30)];
    self.addressTextField.text = self.person.address;
    [self.view addSubview:self.addressTextField];
    
    self.phoneNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 340, 200, 30)];
    self.phoneNumTextField.text = self.person.phoneNumber;
    [self.view addSubview:self.phoneNumTextField];
    
    self.changeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.changeButton.frame = CGRectMake(80, 390, 90, 30);
    [self.changeButton setTitle:@"修改完成" forState:UIControlStateNormal];
    [self.view addSubview:self.changeButton];
    [self.changeButton addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)changeClick:(UIButton *)button {
    Person *person = [[Person alloc] initWithPicImage:self.picImageStr name:self.nameTextField.text address:self.addressTextField.text phoneNumber:self.phoneNumTextField.text];
    [self.delegate sendPerson:person index:self.index];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    UIActionSheet *sheet;
    sheet = [[UIActionSheet alloc] initWithTitle:@"获得图片" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册中选择", @"拍照", nil];
    [sheet showInView:self.view];
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSInteger sourceType = 0;
    sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    UIImagePickerController *imageVC = [[UIImagePickerController alloc] init];
    imageVC.delegate = self;
    imageVC.allowsEditing = YES;
    imageVC.sourceType = sourceType;
    [self presentViewController:imageVC animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self saveImage:image withName:@"image"];
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [sandBox firstObject];
    NSString *imagePath = [path stringByAppendingPathComponent:@"image.plist"];
    self.picImageStr = imagePath;
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
    NSLog(@"%@", imagePath);
    
    [_picImageView setImage:savedImage];
    
}
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName {
    NSData *imageDate = UIImageJPEGRepresentation(currentImage, 1);
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [sandBox firstObject];
    NSString *imagePath = [path stringByAppendingPathComponent:@"image.plist"];
    self.picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", imagePath]];
    self.picImageStr = imagePath;

    [imageDate writeToFile:imagePath atomically:NO];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
